﻿using Kernel.Command.Core.General;
using Kernel.Command.Core.General.Razor;
using Yitter.IdGenerator;

namespace WebAPI.Areas.Core
{
    [ApiVersion("1.0")]
    [ApiVersion("2.0")]
    public class GeneralController : CoreBaseController
    {
        IMediator mediator;

        public GeneralController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        [HttpGet]
        [Route("timestamp"), MapToApiVersion("1.0")]
        public IActionResult GetTimestamp()
        {
            var result = new CommandResult<DateTime> { Data = DateTime.Now };
            return Ok(result);
        }

        [HttpGet]
        [Route("uid"), MapToApiVersion("1.0")]
        public IActionResult GetUniqueId()
        {
            var result = new CommandResult<long> { Data = YitIdHelper.NextId() };
            return Ok(result);
        }

        [HttpGet]
        [Route("guid"), MapToApiVersion("1.0")]
        public IActionResult GetGuid()
        {
            var result = new CommandResult<string> { Data = Guid.NewGuid().ToString("N") };
            return Ok(result);
        }

        [HttpPost]
        [Route("universal"), MapToApiVersion("1.0")]
        public async Task<IActionResult> RunRniversal_V1([FromBody] UniversalCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

        [HttpPost]
        [Route("command"), MapToApiVersion("1.0")]
        public async Task<IActionResult> ExecCommand_V1([FromBody] ExecSqlCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

        [HttpPost]
        [Route("universal"), MapToApiVersion("2.0")]
        public async Task<IActionResult> RunRniversal_V2([FromBody] Dictionary<string, UniversalCommand> commands)
        {
            Dictionary<string, Task<CommandResult<string>>> tasks = new Dictionary<string, Task<CommandResult<string>>>();
            foreach (var command in commands)
            {
                Task<CommandResult<string>> task = mediator.Send(command.Value);
                tasks.Add(command.Key, task);
            }

            Dictionary<string, string> result = new Dictionary<string, string>();
            foreach (var task in tasks)
            {
                var cmdRst = await task.Value;
                result.Add(task.Key, cmdRst.Data);
            }

            return Ok(result);
        }

        [HttpPost]
        [Route("command"), MapToApiVersion("2.0")]
        [TransactionCallHandler]
        public virtual async Task<IActionResult> ExecCommand_V2([FromBody] Dictionary<string, ExecSqlCommand> commands)
        {
            Dictionary<string, Task<CommandResult<object>>> tasks = new Dictionary<string, Task<CommandResult<object>>>();
            foreach (var command in commands)
            {
                Task<CommandResult<object>> task = mediator.Send(command.Value);
                tasks.Add(command.Key, task);
            }

            Dictionary<string, object> result = new Dictionary<string, object>();
            foreach (var task in tasks)
            {
                var cmdRst = await task.Value;
                result.Add(task.Key, cmdRst.Data);
            }
            return Ok(result);
        }

        [HttpPost]
        [Route("code"), MapToApiVersion("1.0")]
        public async Task<IActionResult> OperateCode_V1([FromBody] KernelCodeCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

    }
}
